package com.tjmc.company.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tjmc.company.api.entity.CompanyCollectRecordsEntity;
import com.tjmc.company.mapper.CompanyCollectRecordsMapper;
import com.tjmc.company.service.CompanyCollectRecordsService;
import com.tjmc.recruit.admin.api.entity.PlatformUserInfoEntity;
import com.tjmc.recruit.admin.api.feign.RemoteUserService;
import com.tjmc.recruit.common.core.constant.CommonConstants;
import com.tjmc.recruit.common.core.constant.SecurityConstants;
import com.tjmc.recruit.common.core.exception.CheckedException;
import com.tjmc.recruit.common.core.util.R;
import com.tjmc.recruit.common.security.util.SecurityUtils;
import com.tjmc.recruit.user.vo.ResumeInfoVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 企业收藏表
 *
 * @author recruit
 * @date 2024-12-12 14:23:13
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class CompanyCollectRecordsServiceImpl extends ServiceImpl<CompanyCollectRecordsMapper, CompanyCollectRecordsEntity> implements CompanyCollectRecordsService {

    private final RemoteUserService remoteUserService;

    @Override
    public Page<ResumeInfoVO> selectCollectPage(Page page) {
        Page<ResumeInfoVO> voPage = new Page<>(page.getCurrent(), page.getSize());
        //查询当前用户的企业id
        String username = SecurityUtils.getUser().getName();
        R<PlatformUserInfoEntity> platformedUser = remoteUserService.platformUser(username, SecurityConstants.FROM_IN);
        if (platformedUser.getCode() != CommonConstants.SUCCESS) {
            throw new CheckedException("未查询到企业用户信息");
        }
        Long companyId = platformedUser.getData().getCompanyId();
        if (companyId == null) {
            throw new CheckedException("未查询到企业用户信息");
        }
        // 2. 查询该企业收藏用户简历信息
        return baseMapper.selectCollectResumePage(voPage, companyId);
    }

    @Override
    public CompanyCollectRecordsEntity getCollectResume(Long companyId, Long userId) {
        return baseMapper.selectOne(new LambdaQueryWrapper<CompanyCollectRecordsEntity>()
                .eq(CompanyCollectRecordsEntity::getCompanyId, companyId)
                .eq(CompanyCollectRecordsEntity::getCollectAccountId, userId));
    }
}
